package com.example.translateappjava.Utils;

import android.content.Context;
import com.example.translateappjava.Utils.Post; // 确保路径正确
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class MockDataUtil {

    public static List<Post> loadMockPosts(Context context, String fileName) {
        String jsonString;
        try {
            InputStream is = context.getAssets().open(fileName);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            jsonString = new String(buffer, StandardCharsets.UTF_8);
        } catch (IOException ex) {
            ex.printStackTrace();
            return new ArrayList<>(); // 返回空列表以避免崩溃
        }

        Gson gson = new Gson();
        Type listType = new TypeToken<ArrayList<Post>>(){}.getType();
        List<Post> posts = gson.fromJson(jsonString, listType);
        return posts != null ? posts : new ArrayList<>();
    }

    // 可选: 如果需要根据分类过滤mock数据
    public static List<Post> filterMockPostsByCategory(Context context, String fileName, String category) {
        List<Post> allPosts = loadMockPosts(context, fileName);
        if (category == null || category.isEmpty() || "全部".equals(category) || "all".equalsIgnoreCase(category)) {
            return allPosts;
        }
        // Java 8 Stream API for filtering
        return allPosts.stream()
                .filter(post -> category.equalsIgnoreCase(post.getCategory()))
                .collect(Collectors.toList());
    }
}